<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height: 60px">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click.native="addRole"
          >新增角色</el-button>
        </el-row>

        <!-- 表格 -->
        <el-table :data="roleList">
          <el-table-column label="序号" type="index" width="120" />
          <el-table-column label="姓名" width="240" prop="name" />
          <el-table-column label="角色描述" prop="description" />
          <el-table-column label="操作" width="240" fixed="right">
            <template #default="scope">
              <el-button size="small">分配权限</el-button>
              <el-button size="small" @click.native="upRole(scope.row)">编辑</el-button>
              <el-button
                size="small"
                type="danger"
                @click.native="deleteRole(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页组件 -->
          <el-pagination
            layout="prev,pager,next"
            :current-page="params.page"
            :total="total"
            :page-size="params.pagesize"
            @current-change="pageChange"
          />
        </el-row>
      </el-card>
    </div>
    <!-- 新增弹框 -->
    <el-dialog :title="ruleFrom.id?'编辑角色':'新增角色'" :visible="roleDialogVisible" @close="closeRoleDialog">
      <el-form ref="ruleFrom" label-width="80px" :model="ruleFrom" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="ruleFrom.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="ruleFrom.description" />
        </el-form-item>
      </el-form>
      <el-row slot="footer">
        <el-button size="small" @click.native="closeRoleDialog">取消</el-button>
        <el-button size="small" type="primary" @click="confirmAdd">确定</el-button>
      </el-row>
      <el-dialog />
    </el-dialog>
  </div>
</template>

<script>
import { getRoleListAPI, deleteRoleAPI, addRoleAPI, getRoleDetail, updateRole } from '@/api/settings'
export default {
  name: '',
  // dialog数据

  components: {},
  filters: {},
  data() {
    return {
      // 表单验证的数据
      ruleFrom: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
        description: [{ required: true, message: '请输入角色描述', trigger: 'blur' }]

      },
      // 控制dialog框的开启与关闭 默认false
      roleDialogVisible: false,
      roleList: [],
      params: {
        // 当前页
        page: 1,
        // 每页显示个数
        pagesize: 5
      },
      // 总条目数
      total: 0
    }
  },
  mounted() {
    // 调用函数自执行
    this.getRoleList()
  },
  methods: {
    // 获取角色列表数据
    async getRoleList() {
      const { total, rows } = await getRoleListAPI(this.params)
      this.roleList = rows
      this.total = total
    },
    // 	currentPage 改变时会触发	参数为当前页
    pageChange(page) {
      // 将当前页码赋值给当前的最新页码
      this.params.page = page
      // 重新渲染
      this.getRoleList()
    },
    // 删除
    async  deleteRole(row) {
      // console.log(row.id)
      await deleteRoleAPI(row.id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 重新渲染
        this.getRoleList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 开启弹框
    addRole() {
      this.roleDialogVisible = true
    },
    // 关闭弹框
    closeRoleDialog() {
      this.roleDialogVisible = false
      // 重置表单
      this.$refs.ruleFrom.resetFields()
    },
    // 兜底校验
    confirmAdd() {
      this.$refs.ruleFrom.validate(async valid => {
        if (valid) {
          // todo login
          if (this.ruleFrom.id) {
            await updateRole(this.ruleFrom)
          } else {
            await addRoleAPI(this.ruleFrom)
          }
          this.getRoleList()
          this.$message.success(`${this.ruleFrom.id ? '编辑' : '新增'}成功`)
          this.roleDialogVisible = false
          this.ruleFrom = {
            name: '',
            description: ''
          }
        }
      })
    },
    // this.$message.success(`${this.ruleFrom.id ? '编辑':'新增'}成功`)
    //
    // 编辑
    async upRole(row) {
      // 打开弹框
      this.roleDialogVisible = true
      const res = await getRoleDetail(row.id)
      this.ruleFrom = res
    }

  }
}
</script>

<style lang="less" scoped></style>
